from typing import *
from leetcode.editor.common.node import *


# leetcode submit region begin(Prohibit modification and deletion)
class Solution:
    def maxScore(self, cardPoints: List[int], k: int) -> int:
        n = len(cardPoints)
        s = sum(cardPoints)
        if n == k:
            return s
        k_new = n - k
        ans = 0
        window = 0
        for right, item in enumerate(cardPoints):
            window += item
            left = right - k_new + 1
            if left >= 0:
                ans =  window if ans == 0 else min(ans, window)
                l = cardPoints[left]
                window -= l
                left += 1
        return s - ans
# leetcode submit region end(Prohibit modification and deletion)


if __name__ == '__main__':
    solution = Solution()
    print(solution.maxScore([9,7,7,9,7,7,9], 7))
    # your test code here